//
//  APMyArticlesViewController.m
//  AqualPolo
//
//  Created by dong on 2024/5/9.
//

#import "APMyArticlesViewController.h"
#import "APMyArticlesCell.h"
#import "APDiscoveryDataManager.h"
#import "APLoginManager.h"
#import "APWorksDetailViewController.h"
@interface APMyArticlesViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UILabel *emptyLabel;

@end

@implementation APMyArticlesViewController

- (UILabel *)emptyLabel{
    if (!_emptyLabel) {
        _emptyLabel = [[UILabel alloc] init];
        _emptyLabel.textColor = LYRGBAColor(255, 255, 255, 0.6);
        _emptyLabel.font = [LYFont fontWithSize:12 fontName:LYFontNamepfr];
        _emptyLabel.textAlignment = NSTextAlignmentLeft;
        _emptyLabel.text = @"No Data";
        [self.view addSubview:_emptyLabel];
        [_emptyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.view);
        }];
    }
    return _emptyLabel;
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 10;
        layout.sectionInset = UIEdgeInsetsMake(8, 15, 15, 15);
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = UIColor.clearColor;
        [_collectionView registerClass:[APMyArticlesCell class] forCellWithReuseIdentifier:@"item"];
    }
    return _collectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadNaviBar];
    self.naviBar.titleLabel.text = @"My Articles";
    [self initUI];
    [self refreshData];
}

- (void)refreshData{
    WS(ws);
    [self.view ShowProgress];
    [self.dataArray removeAllObjects];
    APUserModel *userModel = [[APLoginManager shareInstance] userInfo];
    [[APDiscoveryDataManager shareInstance] loadData:^{
        for (APWorksModel *model in [APDiscoveryDataManager shareInstance].dataArray) {
            if ([model.userId isEqualToString:userModel.userId]) {
                [ws.dataArray addObject:model];
            }
        }
        ws.emptyLabel.hidden = self.dataArray.count != 0;
        [ws.collectionView reloadData];
        [ws.view removeProgress];
    }];
}


- (void)deleteButtonDidClick:(UIButton *)sender{
    CGPoint point = sender.center;
    point = [self.collectionView convertPoint:point fromView:sender.superview];
    NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:point];
    APWorksModel *model = [self.dataArray objectAtIndex:indexPath.item];
    [self.view ShowProgress];
    [[APDiscoveryDataManager shareInstance] deleteWorks:model];
    [self.view ShowProgressOnlyLabel:@"delete success"];
    [self refreshData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width =( collectionView.frame.size.width - 40) * 0.5;
    return CGSizeMake(width, 204);
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    APMyArticlesCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    [cell.topButton addTarget:self action:@selector(deleteButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    APWorksModel *model = [self.dataArray objectAtIndex:indexPath.item];
    UIImage *image =  [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",images_path,model.cover]];
    cell.imageView.image = image;
    [cell.avatarImageView sd_setImageWithURL:[NSURL URLWithString:model.avatar]];
    cell.titleLabel.text = model.title;
    cell.nameLabel.text = model.nickname;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    APWorksModel *model = [self.dataArray objectAtIndex:indexPath.item];
    APWorksDetailViewController *detailVC = [[APWorksDetailViewController alloc] init];
    detailVC.worksModel = model;
    [self.navigationController pushViewController:detailVC animated:true];
}

- (void)initUI{
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.naviBar.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
}

@end
